import requests
from  lxml import etree
class film():
    def __init__(self,name,star,releasetime,score):
        self.name=name
        self.star=star
        self.releasetime=releasetime
        self.score=score
#联网版
url='https://maoyan.com/board/4'
headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36'
}
response=requests.get(url=url,headers=headers)
response.encoding=response.apparent_encoding
text=response.text
# 单机版
# text=''
# with open('猫眼100.html','r',encoding='utf-8') as f:
#     text=f.read()
html=etree.HTML(text)
films=[]
for i in range(1,11):
    name=html.xpath(f'//*[@id="app"]/div/div/div[1]/dl/dd[{i}]/div/div/div[1]/p[1]/a/text()')[0].strip().replace('\n', '')
    star=html.xpath(f'//*[@id="app"]/div/div/div[1]/dl/dd[{i}]/div/div/div[1]/p[2]/text()')[0].strip().replace('\n', '')
    releasetime=html.xpath(f'//*[@id="app"]/div/div/div[1]/dl/dd[{i}]/div/div/div[1]/p[3]/text()')[0].strip().replace('\n', '')
    score=html.xpath(f'//*[@id="app"]/div/div/div[1]/dl/dd[{i}]/div/div/div[2]/p/i[1]/text()')[0]+html.xpath(f'//*[@id="app"]/div/div/div[1]/dl/dd[{i}]/div/div/div[2]/p/i[2]/text()')[0]
    fi=film(name,star,releasetime,score)
    films.append(fi)
for index,film in enumerate(films):
    print(f'第{index+1}名',film.name, film.star, film.releasetime, film.score)


for film in films:
    print(film.name,film.star,film.releasetime,film.score)

# filmname=html.xpath('//*[@id="app"]/div/div/div[1]/dl/dd[1]/div/div/div[1]/p[1]/a/text()')[0]
# print(filmname)
# filmname=html.xpath('//*[@id="app"]/div/div/div[1]/dl/dd[1]/div/div/div[1]/p[1]/a')[0]
# print(filmname.text)